import { defineConfig } from '@vue/cli-service';
import { mock } from 'mockjs';
export default defineConfig({
  transpileDependencies: true,
  // 关闭eslint
  lintOnSave: false,
 
  devServer: {
    setupMiddlewares: (middlewares, devServer) => {
      if (!devServer) {
        throw new Error('webpack-dev-server is not defined');
      }
      var app = devServer.app;
      // 模拟地址列表
      var data = mock({
        "list|50": [
          {
            "id|+1": 1,
            name: "@cname",
            tel: "18710950999",
            province: "北京市",
            city: '北京市',
            county: "东城区",
            addressDetail: "天安门广场",
            areaCode: 110101,
            isDefault: false,
            // 控制复选框的状态
            checked:true
          }
        ]
      });
      // 地址列表接口
      app.get('/list', (_, response) => {
        response.send(data.list);
        console.log(response);
        
      });

      // 地址编辑
      app.get('/edit', (req, res) => {
        // 接收参数
        var obj = req.query;
        var index = data.list.findIndex(item => item.id == obj.id);
        if (obj.isDefault) {
          data.list.forEach(item => item.isDefault = false);
        }
        data.list[index] = obj;
        res.send({
          code: 200,
          msg: '操作成功'
        });
      });

      // 地址新增
      app.get('/add', (req, res) => {
        // 接收参数
        var obj = req.query;
        // 随机生成id
        obj.id=Date.now();
        // 要新增的地址是默认地址
        if (obj.isDefault) {
          // 将所有的地址设置为非默认地址
          data.list.forEach(item => item.isDefault = false);
        }
        // 新增的地址在列表的开头显示
        data.list.unshift(obj);
        res.send({
          code: 200,
          msg: '操作成功'
        });

      });

      return middlewares;
    },
     configureWebpack: {
        externals: {
            'AMap': 'AMap' // 高德地图配置
        }
    },
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    
  }
  },
})
module.exports = {
  configureWebpack: {
    externals: {
      'AMap': 'AMap' // 高德地图配置
    }
  },
    
};
